package com.earnings.okhttputils.utils.ui.activity.user;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.UserRechargeLoadData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.BaseFragment;
import com.earnings.okhttputils.utils.ui.fragment.transaction.RechargeOfflineFragment;
import com.earnings.okhttputils.utils.ui.fragment.transaction.RechargeOnlineFragment;
import com.earnings.okhttputils.utils.utils.PagerSlidingTabStripUtils;
import com.earnings.okhttputils.utils.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.Map;

/*
充值-线上线下充值
 */

public class UserBalanceRechargeActivity extends GodLeftHandBaseActivity implements PagerSlidingTabStrip.OnPagerTitleItemClickListener, ViewPager.OnPageChangeListener, View.OnClickListener {


    private PagerSlidingTabStrip tabs;
    private ViewPager viewpager;
    private int type;
    public UserRechargeLoadData rechargeData;
    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void init() {
        setTitle("充值");
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewpager= (ViewPager) findViewById(R.id.viewpager);
        back_view.setOnClickListener( this );
        loadRechargeData();
    }




    public void initTab(){

        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        BaseFragment atFragment= new RechargeOnlineFragment();
        BaseFragment btFragment= new RechargeOfflineFragment();
        atFragment.setTitle("在线充值");
        btFragment.setTitle("线下充值");;
        fragmentList.add(atFragment);
        fragmentList.add(btFragment);




        viewpager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        viewpager.addOnPageChangeListener(this);
        tabs.setViewPager(viewpager);
        PagerSlidingTabStripUtils.setDefault(tabs,this);

        tabs.setOnPagerTitleItemClickListener(this);
    }

    @Override
    public void onBackPressed() {
        back();
    }

    @Override
    public void onClick(View v) {
     //
        int i = v.getId();
        if (i == R.id.back_view) {
            back();

        } else if (i == R.id.top_right_tv) {
            bundleData.putString( "title", "转账记录" );
            //skipActivity( RecordActivity.class );

        } /*else if (i == R.id.turnfriend) {

        } else if (i == R.id.turnuser) {
            skipActivity( TurnUserActivity.class );

        }*/
    }

    @Override
    public void onSingleClickItem(int position) {
        viewpager.setCurrentItem(position);
        type=position;
    }

    @Override
    public void onDoubleClickItem(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        type=position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter{
        ArrayList<Fragment> list;
        public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;

        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {

            return list.get(arg0);
        }



        @Override
        public CharSequence getPageTitle(int position) {
            BaseFragment fragment= (BaseFragment) getItem(position);
            return fragment.getTitle();
        }

    }


    public void back(){
        if(type==0)finish();
        else{
            int nowstep= RechargeOfflineFragment.nowStep;
            if(nowstep==RechargeOfflineFragment.STEP_FIRST)finish();
            else if(nowstep==RechargeOfflineFragment.STEP_SCONED)
                ((RechargeOfflineFragment)getSupportFragmentManager().getFragments().get(1)).runStep(RechargeOfflineFragment.STEP_FIRST);
            else if(nowstep==RechargeOfflineFragment.STEP_THIRD)
                ((RechargeOfflineFragment)getSupportFragmentManager().getFragments().get(1)).runStep(RechargeOfflineFragment.STEP_SCONED);

        }
    }

       /*
获取充值信息
 */

    public void loadRechargeData() {

        Map map = new HttpMap(getContext());

            OkHttpUtils.post().url( HttpUrl.USER_RECHARGE_LOAD_DATA ).params( map ).build().execute( new HttpObjectCallback<UserRechargeLoadData>(getContext() ) {
                @Override
                public void onSuccess(UserRechargeLoadData object, String msg) {
                    rechargeData=object;
                    initTab();


                }

                @Override
                public void onFail(int code, String msg) {
                    ToastUtils.showToast(getContext(),  msg);
                }


            } );

    }
    public void showRechargeOffline(){
        viewpager.setCurrentItem(1);
    }
}
